package com.nullpointer.remotepaint;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class RemoteActivity extends Activity {
	/** Called when the activity is first created. */
	PaintView pv;
	
	SocketThread thread;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		thread = new SocketThread();
		thread.start();
		pv = new PaintView(this, thread.getPrintWriter());
		pv.setBackgroundColor(Color.GREEN);
		Log.d("Kiran ", "In Activity " + pv);
		setContentView(pv);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		Log.d("Kiran ", "In Activity on touch " + pv);

		return pv.onTouchEvent(event);
	}

	class SocketThread extends Thread {
		private Socket socket;
		private OutputStream ooptstrm = null;
		private PrintWriter writer;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			try {
				Log.d("AMOL", "inside run ");
				socket = new Socket("192.168.0.101", 1000);
				Log.d("AMOL", "inside run after socket");
				ooptstrm = socket.getOutputStream();
				Log.d("AMOL", "inside run after getOutputStream");
				writer=new PrintWriter(ooptstrm, true);
				Log.d("AMOL", "inside run after PrintWriter");
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("AMOL", "UnknownHostException " + e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("AMOL", "IOException " + e);
			}catch (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("AMOL", "NullPointerException " + e);
			}
			super.run();

		}

		public PrintWriter getPrintWriter() {

			return writer;
		}
	}
}
